home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsfname.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.4 KB  |  115 lines

  1. /* Copyright (C) 1993, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsfname.c,v 1.2 2000/09/19 19:00:28 lpd Exp $ */
  20. /* File name utilities */
  21. #include "memory_.h"
  22. #include "gserror.h"
  23. #include "gserrors.h"
  24. #include "gsmemory.h"
  25. #include "gstypes.h"
  26. #include "gsfname.h"
  27. #include "gxiodev.h"
  28.  
  29. /* Parse a file name into device and individual name. */
  30. /* The device may be NULL, or the name may be NULL, but not both. */
  31. /* According to the Adobe documentation, %device and %device% */
  32. /* are equivalent; both return name==NULL. */
  33. int
  34. gs_parse_file_name(gs_parsed_file_name_t * pfn, const char *pname, uint len)
  35. {
  36.     uint dlen;
  37.     const char *pdelim;
  38.     gx_io_device *iodev;
  39.  
  40.     if (len == 0)
  41.     return_error(gs_error_undefinedfilename); /* null name not allowed */
  42.     if (pname[0] != '%') {    /* no device */
  43.     pfn->memory = 0;
  44.     pfn->iodev = NULL;
  45.     pfn->fname = pname;
  46.     pfn->len = len;
  47.     return 0;
  48.     }
  49.     pdelim = memchr(pname + 1, '%', len - 1);
  50.     if (pdelim == NULL)        /* %device */
  51.     dlen = len;
  52.     else if (pdelim[1] == 0) {    /* %device% */
  53.     pdelim = NULL;
  54.     dlen = len;
  55.     } else {
  56.     dlen = pdelim - pname;
  57.     pdelim++, len--;
  58.     }
  59.     iodev = gs_findiodevice((const byte *)pname, dlen);
  60.     if (iodev == 0)
  61.     return_error(gs_error_undefinedfilename);
  62.     pfn->memory = 0;
  63.     pfn->iodev = iodev;
  64.     pfn->fname = pdelim;
  65.     pfn->len = len - dlen;
  66.     return 0;
  67. }
  68.  
  69. /* Parse a real (non-device) file name and convert to a C string. */
  70. int
  71. gs_parse_real_file_name(gs_parsed_file_name_t * pfn, const char *pname,
  72.             uint len, gs_memory_t *mem, client_name_t cname)
  73. {
  74.     int code = gs_parse_file_name(pfn, pname, len);
  75.  
  76.     if (code < 0)
  77.     return code;
  78.     if (pfn->len == 0)
  79.     return_error(gs_error_invalidfileaccess);    /* device only */
  80.     return gs_terminate_file_name(pfn, mem, cname);
  81. }
  82.  
  83. /* Convert a file name to a C string by adding a null terminator. */
  84. int
  85. gs_terminate_file_name(gs_parsed_file_name_t * pfn, gs_memory_t *mem,
  86.                client_name_t cname)
  87. {
  88.     uint len = pfn->len;
  89.     char *fname;
  90.  
  91.     if (pfn->iodev == NULL)    /* no device */
  92.     pfn->iodev = iodev_default;
  93.     if (pfn->memory)
  94.     return 0;        /* already copied */
  95.     /* Copy the file name to a C string. */
  96.     fname = (char *)gs_alloc_string(mem, len + 1, cname);
  97.     if (fname == 0)
  98.     return_error(gs_error_VMerror);
  99.     memcpy(fname, pfn->fname, len);
  100.     fname[len] = 0;
  101.     pfn->memory = mem;
  102.     pfn->fname = fname;
  103.     pfn->len = len + 1;        /* null terminator */
  104.     return 0;
  105. }
  106.  
  107. /* Free a file name that was copied to a C string. */
  108. void
  109. gs_free_file_name(gs_parsed_file_name_t * pfn, client_name_t cname)
  110. {
  111.     if (pfn->fname != 0)
  112.     gs_free_const_string(pfn->memory, (const byte *)pfn->fname, pfn->len,
  113.                  cname);
  114. }
  115.